﻿/***********************************************************************
 * <copyright file="PermissionInSiteRepository.cs" company="B/S JSC">
 * -->    Copyright (C) statement. All right reserved
 * </copyright>
 * 
 * Created:   Hoang Bich Son 
 * Email:    sonhb@newman.vn
 * Website:
 * Create Date: Friday, April 11, 2014
 * Usage: 
 * 
 * RevisionHistory: 
 * Date         Author               Description 
 * 
 * ************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using DxMvcApplication.Repositories.Core;
using DxMvcApplication.Repositories.Core.Security;
using DxMvcApplication.SecurityServiceReference;
using NmResources;

namespace DxMvcApplication.Repositories.Implementations.Security
{

    /// <summary>
    /// PermissionInSiteRepository class
    /// </summary>
    public class PermissionInSiteRepository : SecurityRepositoryBase, IPermissionInSiteRepository
    {
        public int GetPageSize()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Gets the list.
        /// </summary>
        /// <param name="criterion">The criterion.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public List<PermissionInSiteDto> GetList(Criterion criterion = null)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Gets the quick search paged.
        /// </summary>
        /// <param name="totalPage">The total page.</param>
        /// <param name="currentPage">The current page.</param>
        /// <param name="keyword">The keyword.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public List<PermissionInSiteDto> GetQuickSearchPaged(ref int totalPage, int currentPage = 1, string keyword = null)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Gets the paged.
        /// </summary>
        /// <param name="totalPage">The total page.</param>
        /// <param name="currentPage">The current page.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public List<PermissionInSiteDto> GetPaged(ref int totalPage, int currentPage = 1)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Gets the specified identifier.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public PermissionInSiteDto Get(int id)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Inserts the specified t.
        /// </summary>
        /// <param name="t">The t.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public string Insert(PermissionInSiteDto t)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Updates the specified t.
        /// </summary>
        /// <param name="t">The t.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public string Update(PermissionInSiteDto t)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Deletes the specified identifier list.
        /// </summary>
        /// <param name="idList">The identifier list.</param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public string Delete(IList<int> idList)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Grants the permission.
        /// </summary>
        /// <param name="permissionId">The permission identifier.</param>
        /// <param name="permissionInSites">The permission in sites.</param>
        /// <returns></returns>
        public string AssignToSite(int permissionId, List<PermissionInSiteDto> permissionInSites)
        {
            var request = new PermissionInSiteRequest().Prepare();
            request.Action = "Insert";
            request.PermissionInSites = permissionInSites.ToArray();
            request.Criteria = new PermissionInSiteCriteria { PermissionId = permissionId };
            var response = SecurityServiceClient.SetPermissionInSite(request);
            Correlate(request, response);
            // These messages are for public consumption. Includes validation errors.
            if (response.Acknowledge == AcknowledgeType.Failure)
                throw new ApplicationException(response.Message);
            return response.MessageCode.MapperResource();
        }

        /// <summary>
        /// Gets the permission in site by permission identifier.
        /// </summary>
        /// <param name="permissionId">The permission identifier.</param>
        /// <returns></returns>
        public List<PermissionInSiteDto> GetPermissionInSiteByPermissionId(int permissionId)
        {
            var request = new PermissionInSiteRequest().Prepare();
            request.LoadOptions = new[] { "PermissionInSiteByPermission" };
            request.Criteria = new PermissionInSiteCriteria { PermissionId = permissionId };
            var response = SecurityServiceClient.GetPermissionInSite(request);
            Correlate(request, response);
            return response.PermissionInSites == null ? null : response.PermissionInSites.ToList();
        }

        /// <summary>
        /// Gets the permission in site by site identifier.
        /// </summary>
        /// <param name="siteId">The site identifier.</param>
        /// <returns></returns>
        public List<PermissionInSiteDto> GetPermissionInSiteBySiteId(int siteId)
        {
            var request = new PermissionInSiteRequest().Prepare();
            request.LoadOptions = new[] { "PermissionInSiteBySite" };
            request.Criteria = new PermissionInSiteCriteria { SiteId = siteId };
            var response = SecurityServiceClient.GetPermissionInSite(request);
            Correlate(request, response);
            return response.PermissionInSites == null ? null : response.PermissionInSites.ToList();
        }
    }
}